#pragma once

#include <iostream>
#include <string>
#include <vector>

#include <ctemplate/template.h>

#include "oj_model.hpp"

namespace dsh_view
{
    using namespace dsh_model;

    const std::string template_path = "./template.html/";

    class View
    {
    public:
        View(){}
        ~View(){}

        bool AllExpandHtml(const std::vector<struct Question>& questions, std::string* html)
        {
            // 题目编号 题目标题 题目难度
            // 形成路径
            std::string src_html = template_path + "all_questions.html";

            // 形成根数据字典
            ctemplate::TemplateDictionary root("all_questions");
            for (const auto& q : questions)
            {
                // 添加子字典，循环放到questions_list中
                ctemplate::TemplateDictionary* sub = root.AddSectionDictionary("questions_list");
                sub->SetValue("number", q.number);
                sub->SetValue("title", q.title);
                sub->SetValue("star", q.star);
            }

            // 获取被渲染的html，传入要渲染的html路径和Strip选型，DO_NOT_STRIP选项意为保持原貌
            ctemplate::Template* tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 开始完成渲染功能
            tpl->Expand(html, &root);

            return true;
        }

        bool OneExpandHtml(const struct Question& q, std::string* html)
        {
            // 形成路径
            std::string src_html = template_path + "one_question.html";

            // 形成字典
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number", q.number);
            root.SetValue("title", q.title);
            root.SetValue("star", q.star);
            root.SetValue("desc", q.desc);
            root.SetValue("pre_code", q.header);

            // 获取被渲染的网页
            ctemplate::Template* tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::Strip::DO_NOT_STRIP);

            // 完成渲染
            tpl->Expand(html, &root);

            return true;
        }

    private:
    };
}